package org.example.quartz.BIP;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.service.LogService;
import org.example.quartz.service.OrgService;
import org.example.quartz.service.UtilService;
import org.example.quartz.task.QuartzTaskJob;
import org.example.quartz.util.DataSourceUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
// 再Job被执行结束后，将会更新JobDataMap，这样下次Job执行后悔使用新的值而不是初始值
@PersistJobDataAfterExecution
// 同一时间将只有一个Job实例被执行，为了避免并发问题导致数据紊乱，建议这两个注解一起使用
@DisallowConcurrentExecution
public class AssistQcQuartz_BIP implements QuartzTaskJob {


    Map<String, Object> gsMap = DataSourceUtil.getGS();
    //全局接口配置
    String IP = (String) gsMap.get("IP");
    String PORT = (String) gsMap.get("PORT");
    String ECC = (String) gsMap.get("ECC");
    String Bearer = (String) gsMap.get("Bearer");

    //日志
    LogService loginfo = SpringUtil.getBean(LogService.class);
    //orgService
    OrgService orgService = SpringUtil.getBean(OrgService.class);
    //orgService
    UtilService utilService = SpringUtil.getBean(UtilService.class);


    /**
     * 辅助期初接口
     * 辅助余额：GSCloud文档3.12
     * http://ip:port/api/jg/syncbddata/v1.0/bdsyncinterface/syncFzye
     **/
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext context) {
        try {
            //传入变量 lastTime 最后抓取时间
            //链接BIP数据库，查询出需要同步的数据，根据TS时间进行判断。
            //TS从中间库表获取到最后一次更新时间，根据此时间进行增量查询推送；
            //中间库日志表记录推送日志，成功、失败、返回信息等（备查）
            //中间库记录最后推送数据的时间，根据时间进行下次查询的条件；
            //查询脚本
            //String YEAR = utilService.getYEAR();
            //获取当前年份
            int YEAR = LocalDateTime.now().getYear();
            String methodName = "AssistQcQuartz";
            JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
            //1、查询时间
            String ts = utilService.getTs(methodName, jdbcTemplate);
            String orgStr = utilService.getGSOrgStr(jdbcTemplate);
            /**
             *科目辅助余额接口
             * 1、业务逻辑
             * 根据传入科目及辅助期初余额数据同步至GSCloud系统科目及辅助期初余额中，该接口只做年初科目及辅助期初余额的初始。
             * 2、数据逻辑
             * 接口需传递明细科目及辅助期初余额全量数据，每次设置都是先删除再重新初始。每一个一级科目的所有明细科目及辅助期初余额数据作为调用一次接口的入参。
             *
             * */
            String selectSQL = "SELECT\n" +
                    "\tgl.ID,\n" +
                    "\tgl.YYZTCODE,\n" +
                    "\tgl.GSZTCODE,\n" +
                    "\tgl.YYCODE,\n" +
                    "\tgl.IYEAR,\n" +
                    "\tgl.CURRENCY,\n" +
                    "\tgl.PKMCODE,\n" +
                    "\tgl.KMCODE,\n" +
                    "\tgl.KMNAME,\n" +
                    "\tgl.FZGS1,\n" +
                    "\tgl.FZ1,\n" +
                    "\tgl.FZCODE1,\n" +
                    "\tgl.FZNAME1,\n" +
                    "\tgl.FZGS2,\n" +
                    "\tgl.FZ2,\n" +
                    "\tgl.FZCODE2,\n" +
                    "\tgl.FZNAME2,\n" +
                    "\tgl.FZGS3,\n" +
                    "\tgl.FZ3,\n" +
                    "\tgl.FZCODE3,\n" +
                    "\tgl.FZNAME3,\n" +
                    "\tgl.FZGS4,\n" +
                    "\tgl.FZ4,\n" +
                    "\tgl.FZCODE4,\n" +
                    "\tgl.FZNAME4,\n" +
                    "\tgl.FZGS5,\n" +
                    "\tgl.FZ5,\n" +
                    "\tgl.FZCODE5,\n" +
                    "\tgl.FZNAME5,\n" +
                    "\tgl.DIRECTION,\n" +
                    "\tgl.BEGINBALANCEAMT,\n" +
                    "\tgl.BALANCE, \n" +
                    "\torg.GSCODE \n" +
                    "FROM\n" +
                    "" + utilService.getGSYY() + ".QC_TABLE gl \n" +
                    " LEFT JOIN " + utilService.getGSYY() + ".yy_org_orgs org on gl.YYZTCODE = org.YYZTCODE \n" +
                    " WHERE gl.PUSHSTATE = '0'";

            //查询数据，根据数据结果推送给GS浪潮
            Session oracleSession = DataSourceUtil.getOracleSession();
            List<Map<String, Object>> query = jdbcTemplate.queryForList(selectSQL);

            //拼装数据，推送给浪潮
            if (query.size() > 0) {
                //部门、人员、往来
                //部门赋值
                String yynumSQL = "SELECT CAST(NUM AS CHAR) AS NUM,GSCODE,TABLENAME,TABLEKEY,CODEKEY,NAMEKEY FROM " + utilService.getGSYY() + ".FREEMAP WHERE istrue = 'DEPT'";
                List<Map<String, Object>> deptMaps = jdbcTemplate.queryForList(yynumSQL);
                String deptnum = (String) deptMaps.get(0).get("GSCODE");
                //往来单位赋值//RELATEDORGID	往来单位编号
                String wlnumSQL = "SELECT CAST(NUM AS CHAR) AS NUM,GSCODE,TABLENAME,TABLEKEY,CODEKEY,NAMEKEY FROM " + utilService.getGSYY() + ".FREEMAP WHERE istrue = 'CANDS'";
                List<Map<String, Object>> wlMaps = jdbcTemplate.queryForList(wlnumSQL);
                String wlnum = (String) wlMaps.get(0).get("GSCODE");
                //人员赋值ACCEMPLOYEEID	核算人员编号
                String rynumSQL = "SELECT CAST(NUM AS CHAR) AS NUM,GSCODE,TABLENAME,TABLEKEY,CODEKEY,NAMEKEY FROM " + utilService.getGSYY() + ".FREEMAP WHERE istrue = 'PERSON'";
                List<Map<String, Object>> ryMaps = jdbcTemplate.queryForList(rynumSQL);
                String rynum = (String) ryMaps.get(0).get("GSCODE");
                //查询所有科目
                //判断借贷
                String kmSQL = "SELECT-- 会计年度（BIP无年度概念，取当前年度）\n" +
                        "TO_CHAR( SYSDATE, 'YYYY' ) AS YEAR,\n" +
                        "-- 账簿编号\n" +
                        "ORG.CODE AS LEDGER,\n" +
                        "-- 核算组织编码\n" +
                        "ORG.CODE AS ACCORGCODE,\n" +
                        "-- 父级科目编码\n" +
                        "A1.CODE AS PARENTCODE,\n" +
                        "-- 科目编号\n" +
                        "B.CODE AS CODE,\n" +
                        "-- 科目名称-- SUBSTR(B.NAME, INSTR(B.NAME, '\\',-1)1) AS NAME,\n" +
                        "A.NAME,\n" +
                        "-- 余额方向 1、借2、贷\n" +
                        "( CASE WHEN B.BALANORIENT = 0 THEN '借' ELSE '贷' END ) AS BALANCEDIR\n" +
                        "FROM\n" +
                        "" + utilService.getOracle() + ".BD_ACCASOA" + utilService.getHZ() + " A\n" +
                        "\tLEFT JOIN " + utilService.getOracle() + ".BD_ACCOUNT" + utilService.getHZ() + " B ON A.PK_ACCOUNT = B.PK_ACCOUNT\n" +
                        "\tLEFT JOIN " + utilService.getOracle() + ".BD_ACCOUNT" + utilService.getHZ() + " A1 ON B.PID = A1.PK_ACCOUNT\n" +
                        "\tLEFT JOIN " + utilService.getOracle() + ".BD_ACCTYPE" + utilService.getHZ() + " BT ON B.PK_ACCTYPE = BT.PK_ACCTYPE\n" +
                        "\tLEFT JOIN " + utilService.getOracle() + ".BD_ACCCHART" + utilService.getHZ() + " AC ON A.PK_ACCCHART = AC.PK_ACCCHART\n" +
                        "\tLEFT JOIN " + utilService.getOracle() + ".ORG_ORGS" + utilService.getHZ() + " ORG ON AC.PK_ORG = ORG.PK_ORG\n" +
                        "\tLEFT JOIN ( SELECT PK_ACCASOA, COUNT( 1 ) AS FZSUM FROM " + utilService.getOracle() + ".BD_ACCASS" + utilService.getHZ() + " GROUP BY PK_ACCASOA ) FZ ON A.PK_ACCASOA = FZ.PK_ACCASOA \n" +
                        "WHERE\n" +
                        "\tORG.CODE IN ( '" + query.get(0).get("YYCODE") + "' ) \n" +
                        "\tAND \n" +
                        "\tA.TS > '1900-01-01' \n" +
                        "ORDER BY\n" +
                        "\tB.ACCLEV ASC,\n" +
                        "\tA.TS ASC,\n" +
                        "\tB.CODE ASC";
                //List<Entity> kmList = oracleSession.query(kmSQL);
                Map<String, List<Map<String, Object>>> QCLIST = query.stream().collect(Collectors.groupingBy(i -> i.get("YYZTCODE") + "," + i.get("PKMCODE") + "," + i.get("IYEAR")));
                for (Map.Entry<String, List<Map<String, Object>>> objectListEntry : QCLIST.entrySet()) {


                    List<Map<String, Object>> value = objectListEntry.getValue();
                    String yyztCode = value.get(0).get("YYZTCODE").toString();
                    String gsCode = value.get(0).get("GSCODE").toString();
                    String pkmCode = value.get(0).get("PKMCODE").toString();
                    String iyear = value.get(0).get("IYEAR").toString();
                    String ids = "";
                    String GsJson = "{\n" +
                            "\t\"YEAR\": \"" + iyear + "\",\n" +
                            "\t\"LEDGER\": \"" + value.get(0).get("GSZTCODE") + "\",\n" +
                            "\t\"ASSDATALIST\": [";
                    for (Map<String, Object> gSmap : value) {
                        BigDecimal BEGINBALANCEAMT = (BigDecimal) gSmap.get("BALANCE");
                        BigDecimal ENDBALANCEAMT = (BigDecimal) gSmap.get("BALANCE");
                        if (gSmap.get("DIRECTION").toString().equals("贷")) {
                            BEGINBALANCEAMT = BigDecimal.valueOf(0.00).subtract(BEGINBALANCEAMT);
                            ENDBALANCEAMT = BigDecimal.valueOf(0.00).subtract(ENDBALANCEAMT);
                        }

                        ids += "'" + gSmap.get("ID") + "',";
                        GsJson += "{\n" +
                                "\t\t\"ACCTITLECODE\": \"" + gSmap.get("KMCODE") + "\",\n" +
                                "\t\t\"CURRENCY\": \"CNY\",\n" +
                                "\t\t\"BEGINBALANCEAMT\": \"" + BEGINBALANCEAMT + "\",\n" +
                                "\t\t\"CURCUMDRAMT\": \"0\",\n" +
                                "\t\t\"CURCUMCRAMT\": \"0\",\n" +
                                "\t\t\"DEBITAMT\": \"0\",\n" +
                                "\t\t\"CREDITAMT\": \"0\",\n" +
                                "\t\t\"ENDBALANCEAMT\": \"" + ENDBALANCEAMT + "\",\n" +
                                "\t\t\"BEGINBALANCEFOR\": \"0\",\n" +
                                "\t\t\"CURCUMDRFOR\": \"0\",\n" +
                                "\t\t\"CURCUMCRFOR\": \"0\",\n" +
                                "\t\t\"DEBITFOR\": \"0\",\n" +
                                "\t\t\"CREDITFOR\": \"0\",\n" +
                                "\t\t\"ENDBALANCEFOR\": \"0\",\n" +
                                "\t\t\"BEGINBALANCEQTY\": \"0\",\n" +
                                "\t\t\"CURCUMDRQTY\": \"0\",\n" +
                                "\t\t\"CURCUMCRQTY\": \"0\",\n" +
                                "\t\t\"DEBITQTY\": \"0\",\n" +
                                "\t\t\"CREDITQTY\": \"0\",\n" +
                                "\t\t\"ENDBALANCEQTY\": \"0\",\n" +
                                "\t\t\"ORDERNUMBER\": \"1\",\n";
                        //判断部门
                        boolean isDept = false;
                        String DeptCode = "";
                        String deptSQL = "";
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS1")) && deptnum.equals(gSmap.get("FZGS1"))) {
                            isDept = true;
                            //转换code
                            DeptCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE1");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS2")) && deptnum.equals(gSmap.get("FZGS2"))) {
                            isDept = true;
                            //转换code
                            DeptCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE2");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS3")) && deptnum.equals(gSmap.get("FZGS3"))) {
                            isDept = true;
                            //转换code
                            DeptCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE3");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS4")) && deptnum.equals(gSmap.get("FZGS4"))) {
                            isDept = true;
                            //转换code
                            DeptCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE4");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS5")) && deptnum.equals(gSmap.get("FZGS5"))) {
                            isDept = true;
                            //转换code
                            DeptCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE5");
                        }
                        if (isDept) {
                            //DeptCode	核算部门编号
                            GsJson += "\t\t\"DEPTCODE\": \"" + DeptCode + "\",\n";
                        } else {
                            GsJson += "\t\t\"DEPTCODE\": \"\",\n";
                        }
                        //判断往来
                        boolean isWL = false;
                        String WLCode = "";
                        String wlSQL = "";
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS1")) && wlnum.equals(gSmap.get("FZGS1"))) {
                            isWL = true;
                            //转换code
                            WLCode = this.getwlCode(oracleSession, wlMaps, "1", gSmap.get("FZCODE1").toString(), gSmap.get("FZNAME1").toString()).replace("$", gsCode);
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS2")) && wlnum.equals(gSmap.get("FZGS2"))) {
                            isWL = true;
                            //转换code
                            WLCode = this.getwlCode(oracleSession, wlMaps, "2", gSmap.get("FZCODE2").toString(), gSmap.get("FZNAME2").toString()).replace("$", gsCode);
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS3")) && wlnum.equals(gSmap.get("FZGS3"))) {
                            isWL = true;
                            //转换code
                            WLCode = this.getwlCode(oracleSession, wlMaps, "3", gSmap.get("FZCODE3").toString(), gSmap.get("FZNAME3").toString()).replace("$", gsCode);
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS4")) && wlnum.equals(gSmap.get("FZGS4"))) {
                            isWL = true;
                            //转换code
                            WLCode = this.getwlCode(oracleSession, wlMaps, "4", gSmap.get("FZCODE4").toString(), gSmap.get("FZNAME4").toString()).replace("$", gsCode);
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS5")) && wlnum.equals(gSmap.get("FZGS5"))) {
                            isWL = true;
                            //转换code
                            WLCode = this.getwlCode(oracleSession, wlMaps, "5", gSmap.get("FZCODE5").toString(), gSmap.get("FZNAME5").toString()).replace("$", gsCode);
                        }
                        if (isWL) {
                            //DeptCode	核算部门编号
                            GsJson += "\t\t\"RELATEDORGCODE\": \"" + WLCode + "\",\n";
                        } else {
                            GsJson += "\t\t\"RELATEDORGCODE\": \"\",\n";
                        }
                        //判断人员
                        boolean isEmployee = false;
                        String employeeCode = "";
                        String employeeSQL = "";
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS1")) && rynum.equals(gSmap.get("FZGS1"))) {
                            isEmployee = true;
                            //转换code
                            employeeCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE1");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS2")) && rynum.equals(gSmap.get("FZGS2"))) {
                            isEmployee = true;
                            //转换code
                            employeeCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE2");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS3")) && rynum.equals(gSmap.get("FZGS3"))) {
                            isEmployee = true;
                            //转换code
                            employeeCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE3");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS4")) && rynum.equals(gSmap.get("FZGS4"))) {
                            isEmployee = true;
                            //转换code
                            employeeCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE4");
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS5")) && rynum.equals(gSmap.get("FZGS5"))) {
                            isEmployee = true;
                            //转换code
                            employeeCode = (String) gSmap.get("YYZTCODE") + gSmap.get("FZCODE5");
                        }
                        if (isEmployee) {
                            GsJson += "\t\t\"ACCEMPLOYEECODE\": \"" + employeeCode + "\",\n";
                        } else {
                            GsJson += "\t\t\"ACCEMPLOYEECODE\": \"\",\n";
                        }
                        String fzSQL = "";
                        String fzCode = "";

                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS1")) && !rynum.equals(gSmap.get("FZGS1")) && !wlnum.equals(gSmap.get("FZGS1")) && !deptnum.equals(gSmap.get("FZGS1"))) {
                            //转换code
                            String qtCode = this.getQTCode(oracleSession, jdbcTemplate, "1", gSmap.get("FZCODE1").toString(), gSmap.get("FZNAME1").toString(), gSmap.get("FZGS1").toString());
                            if (StrUtil.isEmpty(qtCode)) {
                                fzCode = "wu";
                            } else {
                                fzCode = gSmap.get("GSZTCODE") + qtCode;
                            }
                            GsJson += "\t\t\"SPECATECODE" + gSmap.get("FZGS1") + "\": \"" + fzCode + "\",\n";
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS2")) && !rynum.equals(gSmap.get("FZGS2")) && !wlnum.equals(gSmap.get("FZGS2")) && !deptnum.equals(gSmap.get("FZGS2"))) {
                            //转换code
                            String qtCode = this.getQTCode(oracleSession, jdbcTemplate, "2", gSmap.get("FZCODE2").toString(), gSmap.get("FZNAME2").toString(), gSmap.get("FZGS2").toString());
                            if (StrUtil.isEmpty(qtCode)) {
                                fzCode = "wu";
                            } else {
                                fzCode = gSmap.get("GSZTCODE") + qtCode;
                            }
                            GsJson += "\t\t\"SPECATECODE" + gSmap.get("FZGS2") + "\": \"" + fzCode + "\",\n";
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS3")) && !rynum.equals(gSmap.get("FZGS3")) && !wlnum.equals(gSmap.get("FZGS3")) && !deptnum.equals(gSmap.get("FZGS3"))) {
                            //转换code
                            String qtCode = this.getQTCode(oracleSession, jdbcTemplate, "3", gSmap.get("FZCODE3").toString(), gSmap.get("FZNAME3").toString(), gSmap.get("FZGS3").toString());
                            if (StrUtil.isEmpty(qtCode)) {
                                fzCode = "wu";
                            } else {
                                fzCode = gSmap.get("GSZTCODE") + qtCode;
                            }
                            GsJson += "\t\t\"SPECATECODE" + gSmap.get("FZGS3") + "\": \"" + fzCode + "\",\n";
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS4")) && !rynum.equals(gSmap.get("FZGS4")) && !wlnum.equals(gSmap.get("FZGS4")) && !deptnum.equals(gSmap.get("FZGS4"))) {
                            //转换code
                            String qtCode = this.getQTCode(oracleSession, jdbcTemplate, "4", gSmap.get("FZCODE4").toString(), gSmap.get("FZNAME4").toString(), gSmap.get("FZGS4").toString());
                            if (StrUtil.isEmpty(qtCode)) {
                                fzCode = "wu";
                            } else {
                                fzCode = gSmap.get("GSZTCODE") + qtCode;
                            }
                            GsJson += "\t\t\"SPECATECODE" + gSmap.get("FZGS4") + "\": \"" + fzCode + "\",\n";
                        }
                        if (!ObjectUtils.isEmpty(gSmap.get("FZGS5")) && !rynum.equals(gSmap.get("FZGS5")) && !wlnum.equals(gSmap.get("FZGS5")) && !deptnum.equals(gSmap.get("FZGS5"))) {
                            //转换code
                            String qtCode = this.getQTCode(oracleSession, jdbcTemplate, "5", gSmap.get("FZCODE5").toString(), gSmap.get("FZNAME5").toString(), gSmap.get("FZGS5").toString());
                            if (StrUtil.isEmpty(qtCode)) {
                                fzCode = "wu";
                            } else {
                                fzCode = gSmap.get("GSZTCODE") + qtCode;
                            }
                            GsJson += "\t\t\"SPECATECODE" + gSmap.get("FZGS5") + "\": \"" + fzCode + "\",\n";
                        }

                        GsJson += "\t\t\"BIZDICTCODE01\": \"\",\n" +
                                "\t\t\"BIZDICTCODE02\": \"\",\n" +
                                "\t\t\"BIZDICTCODE03\": \"\",\n" +
                                "\t\t\"BIZDICTCODE04\": \"\",\n" +
                                "\t\t\"BIZDICTCODE05\": \"\",\n" +
                                "\t\t\"BIZDICTCODE06\": \"\",\n" +
                                "\t\t\"BIZDICTCODE07\": \"\",\n" +
                                "\t\t\"BIZDICTCODE08\": \"\",\n" +
                                "\t\t\"BIZDICTCODE09\": \"\",\n" +
                                "\t\t\"BIZDICTCODE10\": \"\",\n" +
                                "\t\t\"BIZDICTCODE11\": \"\",\n" +
                                "\t\t\"BIZDICTCODE12\": \"\",\n" +
                                "\t\t\"BIZDICTCODE13\": \"\",\n" +
                                "\t\t\"BIZDICTCODE14\": \"\",\n" +
                                "\t\t\"BIZDICTCODE15\": \"\",\n" +
                                "\t\t\"BIZDICTCODE16\": \"\",\n" +
                                "\t\t\"BIZDICTCODE17\": \"\",\n" +
                                "\t\t\"BIZDICTCODE18\": \"\",\n" +
                                "\t\t\"BIZDICTCODE19\": \"\",\n" +
                                "\t\t\"BIZDICTCODE20\": \"\",\n" +
                                "\t\t\"BIZDICTCODE21\": \"\",\n" +
                                "\t\t\"BIZDICTCODE22\": \"\",\n" +
                                "\t\t\"BIZDICTCODE23\": \"\",\n" +
                                "\t\t\"BIZDICTCODE24\": \"\",\n" +
                                "\t\t\"BIZDICTCODE25\": \"\",\n" +
                                "\t\t\"BIZDICTCODE26\": \"\",\n" +
                                "\t\t\"BIZDICTCODE27\": \"\",\n" +
                                "\t\t\"BIZDICTCODE28\": \"\",\n" +
                                "\t\t\"BIZDICTCODE29\": \"\",\n" +
                                "\t\t\"BIZDICTCODE30\": \"\",\n" +
                                "\t\t\"BIZDICTCODE31\": \"\",\n" +
                                "\t\t\"BIZDICTCODE32\": \"\",\n" +
                                "\t\t\"BIZDICTCODE33\": \"\",\n" +
                                "\t\t\"BIZDICTCODE34\": \"\",\n" +
                                "\t\t\"BIZDICTCODE35\": \"\",\n" +
                                "\t\t\"BIZDICTCODE36\": \"\",\n" +
                                "\t\t\"BIZDICTCODE37\": \"\",\n" +
                                "\t\t\"BIZDICTCODE38\": \"\",\n" +
                                "\t\t\"BIZDICTCODE39\": \"\",\n" +
                                "\t\t\"BIZDICTCODE40\": \"\",\n" +
                                "\t\t\"BIZDICTCODE41\": \"\",\n" +
                                "\t\t\"BIZDICTCODE42\": \"\",\n" +
                                "\t\t\"BIZDICTCODE43\": \"\",\n" +
                                "\t\t\"BIZDICTCODE44\": \"\",\n" +
                                "\t\t\"BIZDICTCODE45\": \"\",\n" +
                                "\t\t\"BIZDICTCODE46\": \"\",\n" +
                                "\t\t\"BIZDICTCODE47\": \"\",\n" +
                                "\t\t\"BIZDICTCODE48\": \"\",\n" +
                                "\t\t\"BIZDICTCODE49\": \"\",\n" +
                                "\t\t\"BIZDICTCODE50\": \"\",\n" +
                                "\t\t\"BIZDICTCODE51\": \"\",\n" +
                                "\t\t\"BIZDICTCODE52\": \"\",\n" +
                                "\t\t\"BIZDICTCODE53\": \"\",\n" +
                                "\t\t\"BIZDICTCODE54\": \"\",\n" +
                                "\t\t\"BIZDICTCODE55\": \"\",\n" +
                                "\t\t\"BIZDICTCODE56\": \"\",\n" +
                                "\t\t\"BIZDICTCODE57\": \"\",\n" +
                                "\t\t\"BIZDICTCODE58\": \"\",\n" +
                                "\t\t\"BIZDICTCODE59\": \"\",\n" +
                                "\t\t\"BIZDICTCODE60\": \"\",\n" +
                                "\t\t\"BIZDICTCODE61\": \"\",\n" +
                                "\t\t\"BIZDICTCODE62\": \"\",\n" +
                                "\t\t\"BIZDICTCODE63\": \"\",\n" +
                                "\t\t\"BIZDICTCODE64\": \"\",\n" +
                                "\t\t\"BIZDICTCODE65\": \"\",\n" +
                                "\t\t\"BIZDICTCODE66\": \"\",\n" +
                                "\t\t\"BIZDICTCODE67\": \"\",\n" +
                                "\t\t\"BIZDICTCODE68\": \"\",\n" +
                                "\t\t\"BIZDICTCODE69\": \"\",\n" +
                                "\t\t\"BIZDICTCODE70\": \"\",\n" +
                                "\t\t\"BIZDICTCODE71\": \"\",\n" +
                                "\t\t\"BIZDICTCODE72\": \"\",\n" +
                                "\t\t\"BIZDICTCODE73\": \"\",\n" +
                                "\t\t\"BIZDICTCODE74\": \"\",\n" +
                                "\t\t\"BIZDICTCODE75\": \"\",\n" +
                                "\t\t\"BIZDICTCODE76\": \"\",\n" +
                                "\t\t\"BIZDICTCODE77\": \"\",\n" +
                                "\t\t\"BIZDICTCODE78\": \"\",\n" +
                                "\t\t\"BIZDICTCODE79\": \"\",\n" +
                                "\t\t\"BIZDICTCODE80\": \"\",\n" +
                                "\t\t\"BIZDICTCODE81\": \"\",\n" +
                                "\t\t\"BIZDICTCODE82\": \"\",\n" +
                                "\t\t\"BIZDICTCODE83\": \"\",\n" +
                                "\t\t\"BIZDICTCODE84\": \"\",\n" +
                                "\t\t\"BIZDICTCODE85\": \"\",\n" +
                                "\t\t\"BIZDICTCODE86\": \"\",\n" +
                                "\t\t\"BIZDICTCODE87\": \"\",\n" +
                                "\t\t\"BIZDICTCODE88\": \"\",\n" +
                                "\t\t\"BIZDICTCODE89\": \"\",\n" +
                                "\t\t\"BIZDICTCODE90\": \"\",\n" +
                                "\t\t\"BIZDICTCODE91\": \"\",\n" +
                                "\t\t\"BIZDICTCODE92\": \"\",\n" +
                                "\t\t\"BIZDICTCODE93\": \"\",\n" +
                                "\t\t\"BIZDICTCODE94\": \"\",\n" +
                                "\t\t\"BIZDICTCODE95\": \"\",\n" +
                                "\t\t\"BIZDICTCODE96\": \"\",\n" +
                                "\t\t\"BIZDICTCODE97\": \"\",\n" +
                                "\t\t\"BIZDICTCODE98\": \"\",\n" +
                                "\t\t\"BIZDICTCODE99\": \"\"\n" +
                                "\t},";
                    }
                    GsJson = GsJson.substring(0, GsJson.lastIndexOf(','));
                    ids = ids.substring(0, ids.lastIndexOf(','));
                    GsJson = GsJson.replace("null", "");
                    GsJson += "]\n" +
                            "}";
                    //调用GS接口，推送给GS浪潮，获取返回值;
                    StopWatch st = new StopWatch();
                    System.out.println(methodName + "开始时间：" + LocalDateTime.now());
                    st.start();
                    String gsUrl = IP + ":" + PORT + "/api/jg/syncbddata/v1.0/bdsyncinterface/syncFzye";
                    String returnMSG = HttpRequest.post(gsUrl)
                            .header("Content-Type", "application/json")
                            .header("Accept-Language", "zh-CHS")
                            .header("X-ECC-Current-Tenant", ECC)
                            .header("Authorization", Bearer).timeout(600000)
                            .body(GsJson).execute().body();
                    System.out.println("凭证返回值:" + returnMSG);
                    st.stop();
                    System.out.println(methodName + "结束时间：" + LocalDateTime.now());
                    System.out.println(methodName + "接口耗时：" + st.getTotalTimeMillis() + "ms");
                    if (!returnMSG.isEmpty()) {
                        JSONObject returnObj = JSONObject.parseObject(returnMSG);
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        //成功
                        if (returnObj.get("code").equals("0")) {
                            int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "成功", GsJson, returnMSG, "", "", gsUrl, formatter.format(LocalDateTime.now()));
                            if (loginsert > 0) {
                                String updateSQL = "update " + utilService.getGSYY() + ".qc_table set PUSHSTATE = '1' WHERE PKMCODE = '" + pkmCode + "' and YYZTCODE = '" + yyztCode + "' and IYEAR = '" + iyear + "'";
                                System.out.println("修改凭证数据状态" + updateSQL);
                                int updateV = jdbcTemplate.update(updateSQL);
                                System.out.println("执行SQL修改方法");
                                if (updateV > 0) {
                                    System.out.println("执行SQL修改方法---成功");
                                } else {

                                    System.out.println("执行SQL修改方法---失败");
                                }
                            } else {
                                System.out.println("日志写入失败！！");
                            }
                            //失败
                        } else if (returnObj.get("code").equals("1")) {
                            int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "失败", GsJson, returnMSG, "", "", gsUrl, formatter.format(LocalDateTime.now()));
                            if (loginsert > 0) {
                                //System.out.println("日志写入成功！！");
                            } else {
                                System.out.println("日志写入失败！！");
                            }
                        } else if (returnObj.get("code") == null && ("").equals(returnObj.get("code"))) {
                            //写入日志表
                            int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", GsJson, returnMSG, "", "", gsUrl, formatter.format(LocalDateTime.now()));
                            if (loginsert > 0) {
                                //System.out.println("日志写入成功！！");
                            } else {
                                System.out.println("日志写入失败！！");
                            }
                        } else  {
                            //写入日志表
                            int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", GsJson, returnMSG, "", "", gsUrl, formatter.format(LocalDateTime.now()));
                            if (loginsert > 0) {
                                //System.out.println("日志写入成功！！");
                            } else {
                                System.out.println("日志写入失败！！");
                            }
                        }
                    }
                }
            } else {
                System.out.println(methodName + "：暂无新数据推送");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //关闭链接
            DataSourceUtil.closeOracleSession();
            DataSourceUtil.closeMysqlSession();
        }
    }


    public String getCode(Session oracleSession, List<Map<String, Object>> maps, String num, String Code, String Name) throws SQLException {
        String sql = "SELECT " + maps.get(0).get("TABLEKEY") + " AS PK FROM " + utilService.getOracle() + "." + maps.get(0).get("TABLENAME") + utilService.getHZ() + " WHERE " + maps.get(0).get("CODEKEY") + " ='" + Code + "' AND " + maps.get(0).get("NAMEKEY") + " ='" + Name + "'";
        List<Entity> query = oracleSession.query(sql);
        String CodeStr = "";
        if (query.size() > 0) {
            String pkStr = query.get(0).get("PK").toString();
            CodeStr = pkStr + Code;
        } else {

        }
        return CodeStr;
    }

    public String getwlCode(Session oracleSession, List<Map<String, Object>> maps, String num, String Code, String Name) throws SQLException {
        String sql = "SELECT  \n" +
                " '03$'||CS.PK_CUST_SUP||CS.CODE  AS CODE\n" +
                "      FROM\n" +
                " " + utilService.getOracle() + ".bd_cust_supplier" + utilService.getHZ() + " cs" +
                "      where" +
                "      cs.code = '" + Code + "'\n" +
                "      and cs.name = '" + Name + "'\n";
        List<Entity> query = oracleSession.query(sql);
        String CodeStr = "";
        if (query.size() > 0) {
            if (ObjectUtils.isEmpty(query.get(0).get("CODE"))) {
                CodeStr = "wu";
            } else {
                CodeStr = query.get(0).get("CODE").toString();
            }
        } else {

        }
        return CodeStr;
    }


    public String getQTCode(Session oracleSession, JdbcTemplate jdbcTemplate, String num, String Code, String Name, String GSNUM) throws SQLException {
        String yynumSQL = "SELECT GSCODE,TABLENAME,TABLEKEY,CODEKEY,NAMEKEY FROM " + utilService.getGSYY() + ".FREEMAP WHERE istrue = 'NO' AND GSCODE = '" + GSNUM + "'";
        List<Map<String, Object>> QTMaps = jdbcTemplate.queryForList(yynumSQL);
        String sql = "SELECT " + QTMaps.get(0).get("TABLEKEY") + " AS PK FROM " + utilService.getOracle() + "." + QTMaps.get(0).get("TABLENAME") + utilService.getHZ() + " WHERE " + QTMaps.get(0).get("CODEKEY") + " ='" + Code + "' AND " + QTMaps.get(0).get("NAMEKEY") + " ='" + Name + "'";
        if (QTMaps.get(0).get("TABLENAME").equals("bd_defdoc")) {
            sql += " AND pk_defdoclist ='" + QTMaps.get(0).get("FZPK") + "'";
        }
        List<Entity> query = oracleSession.query(sql);
        String CodeStr = "";
        if (query.size() > 0) {
            String pkStr = query.get(0).get("PK").toString();
            CodeStr = pkStr + Code;
        } else {

        }
        return CodeStr;
    }
}
